package org.example.managere.repository.feibiao;

import org.example.managere.entity.feibiao.AmKnowledgeOperation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Date;
import java.util.List;

/**
 * 电机实时运行监测数据表Repository
 */
public interface AmKnowledgeOperationRepository extends JpaRepository<AmKnowledgeOperation, Integer>, JpaSpecificationExecutor<AmKnowledgeOperation> {
    // 根据电机ID查询所有运行记录，按采集时间降序排列
    List<AmKnowledgeOperation> findByMotorIdOrderByCollectionTimeDesc(String motorId);
    
    // 查询指定日期范围内的数据
    List<AmKnowledgeOperation> findByCollectionTimeBetween(Date startTime, Date endTime);
    
    // 查询指定电机在特定时间范围内的数据
    List<AmKnowledgeOperation> findByMotorIdAndCollectionTimeBetween(String motorId, Date startTime, Date endTime);
    
    // 统计特定电机的运行记录数量
    Long countByMotorId(String motorId);
    
    // 查询指定电机的最新记录
    @Query("SELECT o FROM AmKnowledgeOperation o WHERE o.motorId = :motorId ORDER BY o.collectionTime DESC LIMIT 1")
    AmKnowledgeOperation findLatestByMotorId(@Param("motorId") String motorId);
} 